//
//  DetailViewController.m
//  EconomyDemo
//
//  Created by qianfeng01 on 15/7/12.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailModel.h"
#import "AFNetworking.h"
#import "HeaderView.h"
#import "FooterView.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
#define kUrl @"http://ipadnews.caijing.com.cn/api/1.0/articles.php?bundleId=cn.com.caijing.android&platform=android4.4.2&appVer=2.4.1&network=wifi&action=detail&id=%d"
#import <WebKit/WebKit.h>
CG_INLINE CGRect
CGRectSetY(CGRect rect, CGFloat y){
    rect.origin.y = y;
    return rect;
}
CG_INLINE UIEdgeInsets
UIEdgeInsetsSetTop(UIEdgeInsets insets, CGFloat top){
    insets.top = top;
    return insets;
}
@interface DetailViewController ()<UIWebViewDelegate>
{
    AFHTTPRequestOperationManager *_manager;
    UIWebView *_webView;
    HeaderView *_headerView;
    FooterView *_footerView;
}
@property (nonatomic,strong) AFHTTPRequestOperationManager *manager;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) HeaderView *headerView;
@property (nonatomic,strong) FooterView *footerView;
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatscrollView];
    [self loadData];
}
-(void)creatscrollView{
    self.headerView=[[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil] lastObject];
    self.footerView=[[[NSBundle mainBundle] loadNibNamed:@"FooterView" owner:nil options:nil] lastObject];
    _webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width,kScreenSize.height-64)];

    
    _headerView.frame = CGRectSetY(_headerView.frame, -_headerView.frame.size.height);
    _webView.scrollView.contentInset = UIEdgeInsetsSetTop(_webView.scrollView.contentInset, _headerView.frame.size.height);
    
    [_webView.scrollView addSubview:_headerView];
    
       _webView.delegate=self;
    [self.view addSubview:_webView];
   
    
}
-(void)loadData{
    _manager=[[AFHTTPRequestOperationManager alloc] init];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    //weakSelf=self;
    NSString *url=[NSString stringWithFormat:kUrl,(int)self.articleId.intValue];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"成功下载");
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            DetailModel *model=[[DetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            //weakSelf.tabBarItem.title=model.columnName;
            _headerView.titleLabel.text=model.title;
            _headerView.authorLabel.text=model.author;
            _footerView.keyLabel.text=model.keywords;
            NSString *jsString = [NSString stringWithFormat:@"<html> \n"
                                  "<head> \n"
                                  "<style type=\"text/css\"> \n"
                                  "body {font-size: %f; font-family: \"%@\"; color: %@;}\n"
                                  "</style> \n"
                                  "</head> \n"
                                  "<body>%@</body> \n"
                                  "</html>", 15.0, nil, nil, model.content];
           NSString *str=[NSString stringWithFormat:@"<head><style>p{text-indent:2em} </style></head>%@",jsString];
            [_webView loadHTMLString:str baseURL:nil];
           // [_webView loadHTMLString:model.content baseURL:nil];
           
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
    
}


@end
